<?php
session_start();

//open connection
$ch = curl_init();

//sort by name.lastSurname, name.firstName if available
$url = 'https://api.sandbox.slcedu.org/api/rest/v1/students?sortBy=studentUniqueStateId';

$token = $_SESSION['access_token'];
$code = $_SESSION['code'];

$auth = sprintf('Authorization: bearer %s', $token);

$headers = array(
  'Content-Type: application/vnd.slc+json',
  'Accept: application/vnd.slc+json',
  $auth);

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

//echo $result;
$json = json_decode($result);

// If response is '401 Unauthorized', redirect back to home page for authentication
if ($json->code == '401') {
  header('Location: index.php');
  die();
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Students</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="js/bootstrap.min.js"></script>
<div class="container">
<ul class="unstyled">
<?php
include 'lib/functions.php';
	foreach ($json as $student) {
	  echo sprintf('<li><a href="snapshot.php?UUID=%s">', $student->id);
	  echo name($student);
	  echo '</a></li>' . "\n";
	}
?>
</ul>
</div>
  </body>
</html>

